/*
	YoghurtGum - 2D Game Engine

	Copyright 2009-2010 Quinten Lansu
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may not 
	use this file except in compliance with the License. You may obtain a copy 
	of the License at 
		
		http://www.apache.org/licenses/LICENSE-2.0 
		
	Unless required by applicable law or agreed to in writing, software 
	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
	License for the specific language governing permissions and limitations 
	under the License. 
*/

#ifndef _ERRORDIRECT3D_H_
#define _ERRORDIRECT3D_H_

#include "..\..\..\YoghurtGum\src\Error.h"

// Direct3D
#include <d3dm.h>
#include <d3dmx.h>
#include <d3dmxerr.h>
#include <d3dmxtex.h>

namespace YoghurtGum
{

	struct ErrorDirect3D
	{
		
		static bool Failed(Flag a_Status)
		{
			if (FAILED(a_Status)) { Check(a_Status); return true; }
			return false;
		}

		static void Check(Flag a_Flag)
		{
			if (!Error::BaseCheck(a_Flag))
			{
				switch (a_Flag)
				{
				case D3DMERR_NOTAVAILABLE:						
					Log::GetSingleton() << "ERROR! Direct3D: Function not available on this device. (D3DMERR_NOTAVAILABLE)" << ENDL;
					break;
				case D3DMERR_ALREADYLOCKED:
					Log::GetSingleton() << "ERROR! Direct3D: Surface was already locked. (D3DMERR_ALREADYLOCKED)" << ENDL;
					break;
				case D3DMERR_INVALIDCALL:
					Log::GetSingleton() << "ERROR! Direct3D: Invalid call or erroneous function parameter. (D3DMERR_INVALIDCALL)" << ENDL;
					break;
				case D3DMERR_INVALIDDEVICE:
					Log::GetSingleton() << "ERROR! Direct3D: Invalid device. (D3DMERR_INVALIDDEVICE)" << ENDL; 
					break;
				case D3DMERR_DRIVERUNSUPPORTED:
					Log::GetSingleton() << "ERROR! Direct3D: Driver is not supported. (D3DMERR_DRIVERUNSUPPORTED)" << ENDL; 
					break;
				case D3DMERR_WRONGTEXTUREFORMAT:
					Log::GetSingleton() << "ERROR! Direct3D: Wrong texture format. (D3DMERR_WRONGTEXTUREFORMAT)" << ENDL; 
					break;
				case D3DMERR_DEVICENOTRESET:
					Log::GetSingleton() << "ERROR! Direct3D: The device was lost and could not be reset." << ENDL;
					/*Log::GetSingleton() << "A fatal error occurred." << ENDL;
					fatalerror = true;
					exit(-123);*/
					break;
				case D3DMERR_DRIVERINTERNALERROR:
					Log::GetSingleton() << "ERROR! Direct3D: Internal driver error. (D3DMERR_DRIVERINTERNALERROR)" << ENDL; 
					break;
				/*case D3DMERR_DEVICEREMOVED:
					Log::GetSingleton() << "ERROR! Direct3D: Device removed. (D3DMERR_DEVICEREMOVED)" << ENDL; 
					break;*/

				case D3DMXERR_NODIRECTDRAWAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: DirectDraw is not (yet) available. (D3DMXERR_NODIRECTDRAWAVAILABLE)" << ENDL; 
					break;
				case D3DMXERR_NODIRECT3DAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: Direct3D is not (yet) available. (D3DMXERR_NODIRECT3DAVAILABLE)" << ENDL; 
					break;
				case D3DMXERR_NODIRECT3DDEVICEAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: Device has not (yet) been initialized. (D3DMXERR_NODIRECT3DDEVICEAVAILABLE)" << ENDL; 
					break;
				case D3DMXERR_NOPRIMARYAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: Primary surface has not (yet) been created. (D3DMXERR_NOPRIMARYAVAILABLE)" << ENDL; 
					break;
				case D3DMXERR_NOZBUFFERAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: Z-buffer is not (yet) available. (D3DMXERR_NOZBUFFERAVAILABLE)" << ENDL; 
					break;
				case D3DMXERR_NOBACKBUFFERAVAILABLE:
					Log::GetSingleton() << "ERROR! Direct3D: Backbuffer is not (yet) available. (D3DMXERR_NOBACKBUFFERAVAILABLE)" << ENDL; 
					break;
				case 0x88760b59 /*D3DMXERR_INVALIDDATA*/:
					Log::GetSingleton() << "ERROR! Direct3D: Invalid data provided. (D3DMXERR_INVALIDDATA)" << ENDL; 
					break;
				case D3DMXERR_NOMEMORY:	
					Log::GetSingleton() << "ERROR! Direct3D: Out of video memory. (D3DMXERR_NOMEMORY)" << ENDL; 
					break;

				default:
					Error::DefaultError(a_Flag);
				}
			}
		}

		static bool LastError()
		{

	#if (YG_OS_TYPE == YG_OS_TYPE_ANDROID)

			return false;

	#elif (YG_OS_TYPE == YG_OS_TYPE_WINDOWSMOBILE)

			DWORD status = GetLastError();
			if (status != 0) { Check(status); return true; }
			return false;

	#endif

		}

	}; // struct ErrorDirect3D

}; // namespace YoghurtGum

#endif